package it.sciuto.turbosmswidget;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class ConfigureActivity extends Activity {
	private ImageButton saveButton;
	private EditText number;
	private EditText message;
	private ImageButton contacts;
	private Spinner spinner;

	private AdView adView;

	public static final String PREFERENCE_FILENAME = "TurboSMS";
	public static final String PREFERENCE_NAME = "name";
	public static final String PREFERENCE_PHONE_NUMBER = "number";
	public static final String PREFERENCE_MESSAGE = "message";
	public static final String PREFERENCE_MULTI_SMS = "multiSMS";

	private int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

	private SharedPreferences settings;
	private Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configure);
		findViewById(R.id.config_layout).requestFocus();

		Bundle extras = getIntent().getExtras();
		if (extras != null)
			appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

		settings = getSharedPreferences(PREFERENCE_FILENAME + appWidgetId, MODE_PRIVATE);
		editor = settings.edit();

		contacts = (ImageButton) findViewById(R.id.contact_button);
		contacts.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				Intent contactIntent = new Intent(ConfigureActivity.this, ContactsActivity.class);
				contactIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
				startActivity(contactIntent);
			}
		});

		number = (EditText) findViewById(R.id.number);
		number.setText(settings.getString(PREFERENCE_PHONE_NUMBER, ""));

		message = (EditText) findViewById(R.id.message);
		message.setText(settings.getString(PREFERENCE_MESSAGE, ""));

		spinner = (Spinner) findViewById(R.id.spinner);
		String[] items = new String[] { "0", "1", "2", "3", "4", "5" };
		ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
		spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(spinnerArrayAdapter);
		spinner.setSelection(getMultiSMS());

		saveButton = (ImageButton) findViewById(R.id.save_button);
		saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				if (!settings.getString(PREFERENCE_PHONE_NUMBER, "").equals(number.getText().toString()))
					editor.putString(PREFERENCE_NAME, "");
				editor.putString(PREFERENCE_PHONE_NUMBER, number.getText().toString());
				editor.putString(PREFERENCE_MESSAGE, message.getText().toString());
				editor.putString(PREFERENCE_MULTI_SMS, (String) spinner.getSelectedItem());
				// System.out.println(number.getText().toString() + ": " +
				// message.getText().toString());
				editor.commit();
				Toast.makeText(ConfigureActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
				// Intent intent = new Intent(ConfigureActivity.this,
				// TurboSMSWidget.class);
				// intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
				TurboSMSWidget.update(ConfigureActivity.this, appWidgetId);
				finish();
			}
		});

		// AdMob
		adView = (AdView) this.findViewById(R.id.adView);
		AdRequest request = new AdRequest();
		// request.addTestDevice(AdRequest.TEST_EMULATOR);
		// request.addTestDevice("8B72A91C88A3CD6A4709DA4294B6E0C0");
		adView.loadAd(request);
	}

	/**
	 * @return
	 */
	public int getMultiSMS() {
		if (settings != null)
			return Integer.parseInt(settings.getString(ConfigureActivity.PREFERENCE_MULTI_SMS, "0"));
		return 0;
	}

	@Override
	protected void onResume() {
		number.setText(settings.getString(PREFERENCE_PHONE_NUMBER, ""));
		message.setText(settings.getString(PREFERENCE_MESSAGE, ""));
		super.onResume();
	}

	@Override
	public void onDestroy() {
		adView.destroy();
		super.onDestroy();
	}
}
